package cn.lxg.service.impl;

import cn.lxg.dao.IUserBookrackDao;
import cn.lxg.pojo.TUserBookrack;
import cn.lxg.service.IUserBookrackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class UserBookrackServiceImpl implements IUserBookrackService {
    @Autowired
    private final IUserBookrackDao userBookrackDao = null;

    @Override
    @Transactional
    public int countDataLine(TUserBookrack userBookrack) {
        return this.userBookrackDao.countDataLine(userBookrack);
    }

    @Override
    @Transactional
    public List<TUserBookrack> getList(Integer limit, Integer page, TUserBookrack userBookrack) {
        /* 计算页面 */
        Integer limitStart = null;
        Integer limitEnd = null;
        if (limit != null && page != null) {
            limitStart = (page - 1) * limit;
            limitEnd = limit;
        }
        return this.userBookrackDao.select(limitStart, limitEnd, userBookrack);
    }

    @Override
    @Transactional
    public TUserBookrack get(Integer id) {
        return this.userBookrackDao.findById(id);
    }

    @Override
    @Transactional
    public String add(TUserBookrack userBookrack) {
        // 校验数据
        if (this.userBookrackDao.countDataLine(userBookrack) != 0)
            return "收藏失败，该文章已经在收藏中";

        userBookrack.setCreateTime(new Date());
        if (this.userBookrackDao.insert(userBookrack))
            return "收藏成功";
        else
            return "收藏失败";
    }

    @Override
    @Transactional
    public String delete(Integer id) {
        if (this.userBookrackDao.deleteById(id))
            return "取消收藏成功";
        else
            return "取消收藏失败";
    }
}
